package com.imti.retrofitweatherdemo.logic;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import com.imti.retrofitweatherdemo.BaseApplication;
import com.imti.retrofitweatherdemo.BuildConfig;
import com.imti.retrofitweatherdemo.logic.WeatherResult.CityWeather;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class WeatherLogic
{

    private static final String TAG = WeatherLogic.class.getSimpleName();

    private BaseApplication application;

    private static WeatherLogic sInstance;

    private IWeatherLogic iWeatherLogic;

    public void setWeatherLogic(IWeatherLogic weatherLogic)
    {
        this.iWeatherLogic = weatherLogic;
    }

    private WeatherLogic(BaseApplication application)
    {
        this.application = application;
    }

    public static WeatherLogic getInstance(BaseApplication application)
    {
        if (null == sInstance)
        {
            sInstance = new WeatherLogic(application);
        }

        return sInstance;
    }

    public void requestWeatherCityList()
    {
//        application.getWeatherRequest().queryCityList(BuildConfig.WEATHER_APPKEY).enqueue(
//            new Callback<WeatherCityModel>()
//            {
//                @Override
//                public void onResponse(Call<WeatherCityModel> call,
//                    Response<WeatherCityModel> response)
//                {
//                    //城市列表
//                    List<WeatherCityResult> cityList = response.body().getResult();
//
//                }
//                @Override
//                public void onFailure(Call<WeatherCityModel> call, Throwable t)
//                {
//                    Log.d(TAG, "weather city list " + t.getMessage());
//                }
//            });

        application.getWeatherRequest().queryCityList(BuildConfig.WEATHER_APPKEY)
            //通过IO线程池发起网络请求
            .subscribeOn(Schedulers.io())
            //取出WeatherCityModel中的List<WeatherCityResult>
            .flatMap(new Func1<WeatherCityModel, Observable<WeatherCityResult>>()
            {
                @Override
                public Observable<WeatherCityResult> call(WeatherCityModel weatherCityModel)
                {
                    //获取到WeatherCityModel
                    return Observable.from(weatherCityModel.getResult());
                }
            })
            //遍历List<WeatherCityResult>
            .map(new Func1<WeatherCityResult, String>()
            {
                @Override
                public String call(WeatherCityResult weatherCityResult)
                {
                    return weatherCityResult.getId().equals("655") ?
                               weatherCityResult.getId() : "";
                }
            })
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<String>()
            {
                @Override
                public void call(String cityId)
                {
                    if ("655".equals(cityId))
                    {
                        iWeatherLogic.onCitylistReady(cityId);
                    }
                }
            });
    }

    public void requestCityWeather(String cityId)
    {
        application.getWeatherRequest().getCityWeatherDetail(BuildConfig.WEATHER_APPKEY, cityId)
            .subscribeOn(Schedulers.io())
            //切换到UI线程
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Action1<WeatherResult>()
            {
                @Override
                public void call(WeatherResult weatherResult)
                {
                    iWeatherLogic.onCityWeatherResult(weatherResult.getResult());
                }
            });
    }

}
